package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.common.Result;
import com.amsp.oa.oademo.entity.KaoqinWenjianDistribution;
import com.amsp.oa.oademo.service.KaoqinWenjianDistributionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Api(tags = "考勤文件发放管理")
@RestController
@RequestMapping("/api/kaoqin-wenjian")
public class KaoqinWenjianDistributionController {

    private static final Logger log = LoggerFactory.getLogger(KaoqinWenjianDistributionController.class);

    @Autowired
    private KaoqinWenjianDistributionService distributionService;

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<IPage<KaoqinWenjianDistribution>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String fileNumber,
            @RequestParam(required = false) String fileName) {
        return Result.ok(distributionService.getPage(current, size, fileNumber, fileName));
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询")
    public Result<KaoqinWenjianDistribution> getById(@PathVariable Long id) {
        try {
            KaoqinWenjianDistribution distribution = distributionService.getByIdInfo(id);
            if (distribution == null) {
                return Result.error("未找到相关记录");
            }
            return Result.ok(distribution);
        } catch (Exception e) {
            log.error("查询文件发放记录失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    @PostMapping("/save")
    @ApiOperation("保存或更新")
    public Result<Boolean> saveOrUpdate(@RequestBody KaoqinWenjianDistribution distribution) {
        return Result.ok(distributionService.saveOrUpdateWithDetails(distribution));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    public Result<Boolean> removeById(@PathVariable Long id) {
        return Result.ok(distributionService.removeById(id));
    }
} 